//
//  STStatusOriginalView.m
//  OCVVeibo
//
//  Created by leergou on 16/7/27.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "STStatusOriginalView.h"
#import "STStatusDTO.h"
#import "STStatusVM.h"
#import "STStatusPictureInfo.h"
#import "STStatusPictureView.h"

static float Margin = 10;
static float cellWidth = 80;

@interface STStatusOriginalView ()

// 头像
@property (nonatomic,strong) UIImageView *headImageView;
// 昵称
@property (nonatomic,strong) UILabel *nameLabel;
// 会员等级
@property (nonatomic,strong) UIImageView *vipImageView;
// 认证
@property (nonatomic,strong) UIImageView *avatarImageView;
// 发布时间
@property (nonatomic,strong) UILabel *timeLabel;
// 来源
@property (nonatomic,strong) UILabel *sourceLabel;
// 微博内容
@property (nonatomic,strong) UILabel *contentLabel;
// 配图
@property (nonatomic,strong) STStatusPictureView *pictureView;
// 记录原创微博底部约束
@property (nonatomic,strong) MASConstraint *originalViewBottomConstraint;

@end

@implementation STStatusOriginalView

- (instancetype)init{
    
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 重写 statusVM 模型set 方法,分发数据
- (void)setStatusVM:(STStatusVM *)statusVM{
    _statusVM = statusVM;
        self.nameLabel.text = statusVM.status.user.name;
//        self.sourceLabel.text = statusVM.status.source; // 后续处理,否则显示太长了
    self.sourceLabel.attributedText = statusVM.sourceAttributeText;
//    NSLog(@"%@",statusVM.sourceAttributeText);
        self.contentLabel.text = statusVM.status.text;
        self.timeLabel.text = [NSDate getAStringWithDateString:statusVM.status.created_at];
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:statusVM.status.user.profile_image_url]];
    [self.vipImageView setImage:statusVM.vipImage];
    [self.avatarImageView setImage:statusVM.verifiedImage];
        //MARK: 是否有配图
        // 记得首先要卸载一下,否则约束冲突
        [self.originalViewBottomConstraint uninstall];
        if (statusVM.status.pic_urls.count) {
            // 赋值
            //        NSLog(@"pittureView:%ld",status.pic_urls.count);
            self.pictureView.statusPictureList = statusVM.status.pic_urls;
            // 更新约束 (自身 bottom 约束)
            [self makeConstraints:^(MASConstraintMaker *make) {
                self.originalViewBottomConstraint = make.bottom.equalTo(self.pictureView.bottom).offset(Margin);
            }];
            // 显示配图
            self.pictureView.hidden = NO;
        } else {
            // 更新约束 (自身 bottom 约束)
            [self makeConstraints:^(MASConstraintMaker *make) {
                self.originalViewBottomConstraint = make.bottom.equalTo(self.contentLabel.bottom).offset(Margin);
            }];
            // 隐藏配图
            self.pictureView.hidden = YES;
        }

}

#pragma mark - 重写模型 set 方法,给子控件赋值
//- (void)setStatus:(STStatusDTO *)status{
//    _status = status;
//    self.nameLabel.text = status.user.name;
//    self.timeLabel.text = status.; // 最后处理,否则显示太长了
//    self.sourceLabel.text = status.source; // 后续处理,否则显示太长了
//    self.contentLabel.text = status.text;
//    self.timeLabel.text = [NSDate getAStringWithDateString:status.created_at];
//    self.sourceLabel.text = @"来源";
//    
//    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:status.user.profile_image_url]];
//    
//    //MARK: 是否有配图
//    // 记得首先要卸载一下,否则约束冲突
//    [self.originalViewBottomConstraint uninstall];
//    if (status.pic_urls.count) {
//        // 赋值
////        NSLog(@"pittureView:%ld",status.pic_urls.count);
//        self.pictureView.statusPictureList = status.pic_urls;
//        // 更新约束 (自身 bottom 约束)
//        [self makeConstraints:^(MASConstraintMaker *make) {
//            self.originalViewBottomConstraint = make.bottom.equalTo(self.pictureView.bottom).offset(Margin);
//        }];
//        // 显示配图
//        self.pictureView.hidden = NO;
//    } else {
//        // 更新约束 (自身 bottom 约束)
//        [self makeConstraints:^(MASConstraintMaker *make) {
//            self.originalViewBottomConstraint = make.bottom.equalTo(self.contentLabel.bottom).offset(Margin);
//        }];
//        // 隐藏配图
//        self.pictureView.hidden = YES;
//    }
//}

#pragma mark - 设置视图
- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    // 添加子控件
    [self addSubview:self.headImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.vipImageView];
    [self addSubview:self.avatarImageView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];
    
    // 添加约束
    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(Margin);
        make.size.equalTo(CGSizeMake(35, 35));
    }];
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(Margin);
        make.leading.equalTo(self.headImageView.trailing).offset(Margin);
    }];
    
    [self.vipImageView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.nameLabel.trailing).offset(Margin);
        make.centerY.equalTo(self.nameLabel);
    }];
    
    [self.avatarImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.headImageView.trailing);
        make.centerY.equalTo(self.headImageView.bottom);
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.headImageView.trailing).offset(Margin);
        make.bottom.equalTo(self.headImageView.bottom);
    }];
    
    [self.sourceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.timeLabel.trailing).offset(Margin);
        make.bottom.equalTo(self.headImageView.bottom);
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView.bottom).offset(Margin);
        make.leading.equalTo(self.headImageView.leading);
    }];
    
    [self.pictureView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.bottom).offset(Margin);
        make.leading.equalTo((SCREEN_WIDTH - (3 * cellWidth + 2 * Margin)) * 0.5);
//        make.size.equalTo(CGSizeMake(250, 250));
    }];
    
    // 设置原创微博本身的约束 -> 根据微博内容高度,自动调整
    [self makeConstraints:^(MASConstraintMaker *make) {
       self.originalViewBottomConstraint = make.bottom.equalTo(self.pictureView.bottom).offset(Margin);
    }];

}

#pragma mark - 设置约束--用 Masonry 做约束,约束直接跟在 添加 后面,若写在 layoutSubviews 中,有时候会导致布局有错误,如果想直接修改frame,可以在 layoutSubviews 中修改
- (void)layoutSubviews{
    [super layoutSubviews];
    
//    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
//        make.top.leading.equalTo(Margin);
//        make.size.equalTo(CGSizeMake(35, 35));
//    }];
//    
//    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(Margin);
//        make.leading.equalTo(self.headImageView.trailing).offset(Margin);
//    }];
//    
//    [self.vipImageView makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.nameLabel.trailing).offset(Margin);
//        make.centerY.equalTo(self.nameLabel);
//    }];
//    
//    [self.avatarImageView makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.headImageView.trailing);
//        make.centerY.equalTo(self.headImageView.bottom);
//    }];
//    
//    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.headImageView.trailing).offset(Margin);
//        make.bottom.equalTo(self.headImageView.bottom);
//    }];
//    
//    [self.sourceLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.equalTo(self.timeLabel.trailing).offset(Margin);
//        make.bottom.equalTo(self.headImageView.bottom);
//    }];
//    
//    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.headImageView.bottom).offset(Margin);
//        make.leading.equalTo(self.headImageView.leading);
//    }];
//    
//    // 设置原创微博本身的约束 -> 根据微博内容高度,自动调整
//    [self makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.contentLabel.bottom).offset(Margin);
//    }];
    
}


#pragma mark - lazy
- (UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] initWithImageName:@"avatar_default"];
    }
    return _headImageView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithTextColor:[UIColor blackColor] textFont:15 maxWidth:0];
    }
    return _nameLabel;
}

- (UIImageView *)vipImageView{
    if (!_vipImageView) {
        _vipImageView = [[UIImageView alloc] initWithImageName:@"common_icon_membership"];
    }
    return _vipImageView;
}

- (UIImageView *)avatarImageView{
    if (!_avatarImageView) {
        _avatarImageView = [[UIImageView alloc] initWithImageName:@"avatar_vgirl"];
    }
    return _avatarImageView;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithTextColor:ThemeColor textFont:10 maxWidth:0];
    }
    return _timeLabel;
}

- (UILabel *)sourceLabel{
    if (!_sourceLabel) {
        _sourceLabel = [[UILabel alloc] initWithTextColor:ThemeColor textFont:10 maxWidth:0];
    }
    return _sourceLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithTextColor:[UIColor darkGrayColor] textFont:15 maxWidth:(SCREEN_WIDTH - 20)];
    }
    return _contentLabel;
}

- (STStatusPictureView *)pictureView{
    if (!_pictureView) {
        _pictureView = [[STStatusPictureView alloc] init];
    }
    return _pictureView;
}






@end











